package com.eachonline.smart.web.image;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.eachonline.core.JsonResult;
import com.eachonline.oss.OssConstant;
import com.eachonline.oss.OssService;
import com.eachonline.util.DateUtil;
import com.eachonline.util.StringUtil;
import com.eachonline.util.UUIDUtil;

@Controller
@RequestMapping("/image")
public class CommonImageController {
	

	public static String PIC_URL="http://139.196.194.93:8080/smart/image";
	
	@Autowired
	private OssService ossService;
	/**
	 * 上传图片
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/uploadImg", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String,Object> uploadImg(@RequestParam("imgFile") MultipartFile imgFile,
			HttpServletResponse response) {
		StringBuffer key = new StringBuffer();
		String str=UUIDUtil.getUUID();
		key.append(DateUtil.formatPureStandardDate(new Date()));
		key.append("/");
		key.append(str.substring(0, 6));
		key.append("/");
		key.append(str);
		System.out.println(key.toString());
		ossService.upload(OssConstant.NAMESPACE_BUSINESS, key.toString(),
				imgFile, "image/jpeg");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("key", key.toString());
		map.put("keyUrl",PIC_URL + "/getPic?key="+key.toString());
		return map;
	}
	
	
	/**
	 * 上传图片
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/uploadImgText", method = { RequestMethod.POST },produces="text/html;charset=UTF-8")
	@ResponseBody
	public Map<String,Object> uploadImgText(@RequestParam("imgFile") MultipartFile imgFile,
			HttpServletResponse response) {
		StringBuffer key = new StringBuffer();
		String str=UUIDUtil.getUUID();
		key.append(DateUtil.formatPureStandardDate(new Date()));
		key.append("/");
		key.append(str.substring(0, 6));
		key.append("/");
		key.append(str);
		System.out.println(key.toString());
		ossService.upload(OssConstant.NAMESPACE_BUSINESS, key.toString(),
				imgFile, "image/jpeg");
		response.setContentType("text/html");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("key", key.toString());
		map.put("keyUrl",PIC_URL + "/getPic?key="+key.toString());
		return map;
	}
	
	
	/**
	 * 富文本编辑器图片上传
	 * 
	 * @param file
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/upload", method = { RequestMethod.POST })
	@ResponseBody
	public void editorUploadImg(@RequestParam("file") MultipartFile file,HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
		response.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
		response.setHeader("Access-Control-Allow-Origin", "*");
	
		StringBuffer key = new StringBuffer();
		String str=UUIDUtil.getUUID();
		key.append(DateUtil.formatPureStandardDate(new Date()));
		key.append("/");
		key.append(str.substring(0, 6));
		key.append("/");
		key.append(str);
		System.out.println(key.toString());
		ossService.upload(OssConstant.NAMESPACE_BUSINESS, key.toString(),
				file, "image/jpeg");
		/*map.put("key", key.toString());
		  map.put("keyUrl", BaseModel.PIC_URL + "/img/getPic?key="+key.toString());
		*/
		 //获取图片url地址
        String imgUrl =PIC_URL + "/getPic?key="+key.toString();
        PrintWriter out = response.getWriter();
        out.print(imgUrl);  //返回url地址
        out.flush();
        out.close();
	}
	
	
	/**
	 * 获取图片
	 * 
	 * @param key
	 * @param response
	 */
	@RequestMapping(value = "/getPic", method = { RequestMethod.GET })
	public void getPic(String key, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		try {
			ServletOutputStream outputStream = response.getOutputStream();
			OSSObject ossObject = null;
			if ("null".equals(key) || null == key || "".equals(key)) {
				ossObject = ossService.download(
						OssConstant.NAMESPACE_BUSINESS, "houseIcon.jpg");
			} else {
				ossObject = ossService.download(
						OssConstant.NAMESPACE_BUSINESS, key);
			}
			if (ossObject == null) {
				return;
			}

			ObjectMetadata metadata = ossObject.getObjectMetadata();
			response.setContentType(metadata.getContentType());

			InputStream objectContent = ossObject.getObjectContent();
			IOUtils.copy(objectContent, outputStream);

			objectContent.close();
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/deleteImage", method = { RequestMethod.POST })
	@ResponseBody
	public JsonResult deleteImage(String picHouseUrl,
			HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		if (StringUtil.isNotEmpty(picHouseUrl)) {
			ossService.delete(OssConstant.NAMESPACE_BUSINESS, picHouseUrl);
			return new JsonResult(true, "删除成功");
		}
		return new JsonResult(false);
	}
	

}
